import { reactive, toRefs } from 'vue';

// 验证码逻辑
const timeInfo = reactive({
    count: 0,
    timer: -1,
    codeShow: true,
});

const clearTime = () => {
    clearInterval(timeInfo.timer);
    timeInfo.timer = -1;
};

function handleGetCode() {
    const TIME_COUNT = 60;
    if (timeInfo.timer === -1) {
        timeInfo.count = TIME_COUNT;
        timeInfo.codeShow = false;
        timeInfo.timer = setInterval(() => {
            if (timeInfo.count > 0 && timeInfo.count <= TIME_COUNT) {
                timeInfo.count--;
            } else {
                timeInfo.codeShow = true;
                clearTime();
            }
        }, 1000);
    }
}

export default {...toRefs(timeInfo), handleGetCode, clearTime };
